package year2021.month0567;

import java.util.*;
import java.util.stream.Collectors;

public class MainTest {

    public static void main(String[] args){
//        Info info = new Info();
//        info.setName("??");
//        String sourceAlias = Optional.ofNullable(info.getName()).map(String::toString).orElse("");
//        Info info = null;
//        String sourceAlias = Optional.ofNullable(info).map(Info::getName).orElse("");
//        System.out.println(sourceAlias);

//        Map<String, String> map = new HashMap<>();
//        map.put("1", "1");
//        map.put("", "12");

        List<Info> infoList = new ArrayList<>();
        infoList.add(new Info().setName("").setAge(10));
        infoList.add(new Info().setName("").setAge(5));
        infoList.add(new Info().setName("1").setAge(10));
        infoList.add(new Info().setName("2").setAge(21));
        infoList.add(new Info().setName("666").setAge(60));
        infoList.add(new Info().setName("666").setAge(66));

//        Map<String, Info> map = infoList.stream().collect(Collectors.toMap(Info::getName, Function.identity()));

//        Map<String, List<Integer>> listMap1 = infoList.stream().collect(Collectors.toMap(
//                Info::getName,
//                o -> new ArrayList<Integer>(){{add(o.getAge());}},
//                (List<Integer> oldList, List<Integer> newList) -> {
//                    oldList.addAll(newList);
//                    return oldList;
//                }
//        ));

        Map<String, List<Info>> listMap2 = infoList.stream().collect(Collectors.toMap(
                Info::getName,
                o -> new ArrayList<Info>(){{add(o);}},
                (List<Info> oldList, List<Info> newList) -> {
                    oldList.addAll(newList);
                    return oldList;
                }
        ));

//        System.out.println(map);
        System.out.println(listMap2);

    }

}

class Info{
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public Info setName(String name) {
        this.name = name;
        return this;
    }

    public int getAge() {
        return age;
    }

    public Info setAge(int age) {
        this.age = age;
        return this;
    }
}